package co.com.javeriana.aes.oms.orders;

import co.com.javeriana.aes.oms.common.BaseBean;
import co.com.javeriana.aes.oms.common.entity.Orders;
import co.com.javeriana.aes.oms.constants.NavRules;
import co.com.javeriana.aes.oms.orders.service.OrdersServiceBeanLocal;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import org.primefaces.event.SelectEvent;
import org.primefaces.event.UnselectEvent;

/**
 *
 * @author Jonathan Fernandez <jonathan.fvargas@gmail.com>
 */
@ManagedBean(name = "OrdersBean")
@SessionScoped
public class OrdersBean extends BaseBean {

    @EJB
    private OrdersServiceBeanLocal localservice;
    private List<Orders> dtos, ordersByProduct;
    private Orders selected;
    private String orderId, productID;
    private String currentView;
    private boolean renderById = false;

    @PostConstruct
    public void init() {
        dtos = localservice.getOrders();
        ordersByProduct = new ArrayList<Orders>();
        currentView = new String();
        orderId = new String();
        productID = new String();
        renderById = false;
        selected = null;
    }

    public String getCurrentView() {
        return currentView;
    }

    public void setCurrentView(String currentView) {
        this.currentView = currentView;
    }
    
    public String getProductID() {
        return productID;
    }

    public void setProductID(String productID) {
        this.productID = productID;
    }

    public boolean isRenderById() {
        return renderById;
    }

    public void setRenderById(boolean renderById) {
        this.renderById = renderById;
    }

    public String getOrderId() {
        return orderId;
    }

    public void setOrderId(String orderId) {
        this.orderId = orderId;
    }

    public Orders getSelected() {
        return selected;
    }

    public void setSelected(Orders selected) {
        this.selected = selected;
    }

    public List<Orders> getOrdersByProduct() {
        return ordersByProduct;
    }

    public void setOrdersByProduct(List<Orders> ordersByProduct) {
        this.ordersByProduct = ordersByProduct;
    }
    
    public List<Orders> getDtos() {
        return dtos;
    }

    public void setDtos(List<Orders> dtos) {
        this.dtos = dtos;
    }

    public void findOrders() {
        
        //set the back view
        currentView = NavRules.GO_TO_ORDERS_BY_ID.name();

        try {

            if (orderId.isEmpty()) {
                init();
            } else {

                selected = localservice.getOrderById(Integer.valueOf(orderId));

                if (selected != null) {
                    dtos.clear();
                    //dtos.add(selected);
                }else{
                    addMessage("No existe la orden consultada");
                    orderId = "";
                }
            }

        } catch (Exception e) {
            //show message no info
            addMessage("No existe la orden consultada");
            orderId = "";
        }
    }

    public void findOrdersByPrpductId() {
        
        //set the back view
        currentView = NavRules.GO_TO_ORDERS_BY_PRODUCT_ID.name();

        try {

            if (productID.isEmpty()) {
                init();
            } else {

                ordersByProduct = localservice.getOrderByProductID(Integer.valueOf(productID));

                if (ordersByProduct==null || ordersByProduct.isEmpty()){
                    addMessage("No existen órdenes asociadas al producto");
                }
                
                
            }

        } catch (Exception e) {
            //show message no info
            addMessage("No existen órdenes asociadas");
            productID = "";
        }
    }

    public String goToDetails() {
        
        //to avoid lazy load
        Integer id = selected.getOrdid();
        selected = localservice.getOrderById(id);
        return NavRules.GO_TO_ORDERS_DETAILS.name();
    }
    
    
    public String goToDetailsByProduct() {
        
        //to avoid lazy load
        Integer id = selected.getOrdid();
        selected = localservice.getOrderById(id);
        return NavRules.GO_TO_ORDERS_DETAILS.name();
    }

    public String back() {
        renderById = false;
        return NavRules.GO_TO_ORDERS_BY_ID.name();
    }
    
    public String backOrderByProduct() {
        renderById = false;
        return NavRules.GO_TO_ORDERS_BY_PRODUCT_ID.name();
    }
    
    
    public String backToMainPageByProducts() {
        return NavRules.GO_TO_ORDERS_BY_PRODUCT_ID.name();
    }

    public void onRowSelect(SelectEvent event) {
        renderById = true;
    }

    public void onRowUnselect(UnselectEvent event) {
        renderById = false;
    }

}
